using UnityEngine;
using UnityEngine.Rendering;

[ExecuteInEditMode]
public class TextureChanger : MonoBehaviour
{
	private int _index;

	public int Index;

	private float _greyScale;

	public float GreyScale = 0.5f;

	private Color _lastAdditionalColor;

	public Color AdditionalColor;

	public bool alwaysLoaded;

	public bool disableForWeakDevices;

	[HideInInspector]
	public MeshFilter _meshFilter;

	[HideInInspector]
	public MeshCollider meshCollider;

	[HideInInspector]
	public MeshRenderer cachedMeshRenderer;

	[HideInInspector]
	public SectorCreate parentSector;

	[HideInInspector]
	public bool refCountIncreased;

	public string meshNameFromDB;

	private void Awake()
	{
		if (Application.isPlaying && Device.isWeakDevice && disableForWeakDevices)
		{
			base.gameObject.SetActive(false);
		}
	}

	private void Start()
	{
		if (Application.isPlaying)
		{
			GetCompForTextChan();
			cachedMeshRenderer.shadowCastingMode = ShadowCastingMode.Off;
			if (GameController.instance != null && (GameController.instance.curTypeGame == TypeGame.deathmath || GameController.instance.curTypeGame == TypeGame.race || GameController.instance.curTypeGame == TypeGame.TitanfallV2) && _meshFilter != null && _meshFilter.sharedMesh != null && meshCollider != null && meshCollider.sharedMesh == null)
			{
				meshCollider.sharedMesh = _meshFilter.sharedMesh;
			}
			base.enabled = false;
		}
	}

	private void OnDestroy()
	{
		ClearMesh();
	}

	public void ClearMesh()
	{
		if (Application.isPlaying && BCWResourceManager.instance != null && refCountIncreased)
		{
			BCWResourceManager.instance.UnloadMesh(meshNameFromDB);
			refCountIncreased = false;
		}
		if (_meshFilter != null)
		{
			_meshFilter.sharedMesh = null;
		}
		if (meshCollider != null)
		{
			meshCollider.sharedMesh = null;
		}
	}

	public void GetCompForTextChan()
	{
		if (_meshFilter == null)
		{
			_meshFilter = GetComponent<MeshFilter>();
		}
		if (meshCollider == null)
		{
			meshCollider = GetComponent<MeshCollider>();
		}
		if (cachedMeshRenderer == null)
		{
			cachedMeshRenderer = GetComponent<MeshRenderer>();
		}
	}
}
